<?php 

Class wxruleModel extends appModel
{
	private $option=array();
	private $loop_num=0;
	public $db;
	
	/**
	 * 获取微信配置信息
	 * @return 
	 */
	public function getWeixin()
	{
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxareply'OR op_key='wxdreply'";
		$res=$this->getAll($sql);
		$arr=array();
		
		$arr['wxdreply']=$res[0]['op_value'];
		$arr['wxareply']=$res[1]['op_value'];

		return  $arr;
	}
	
	/**
	 * 保存微信的设置
	 * @param $info
	 * @return 
	 */
	public function saveWeixin($info)
	{
		foreach($info as $k => $v)
		{
			if(!($v === ''))
			{
				$sql="UPDATE ".$this->table('options')." SET op_value='$v' WHERE op_key='$k'";
				$res=$this->query($sql);
			}
		}
		return $res;
	}
   
	/**
	 * 获取规则详情
	 * @param $id
	 * @return
	 */
   	public function get_rule_info($id)
   	{
   		$sql = " SELECT id,name,status,type FROM ".$this->table('rule')." WHERE id='".$id."'";
   		$info = $this->getRow($sql);
   	
   		if($info['type'] == 1)
   		{
   			$sql = " SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
   			$info['content'] = $this->getOne($sql);
   		}
   		else 
   		{
   			$sql = " SELECT id ,title ,description ,thumb ,url  FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
   			$result = $this->getAll($sql);
   			$info['pic_img'] = $result;
   		}
   		return $info;
   	}
   
 	/**
 	 * 删除微信规则
 	 * @param unknown $id
 	 * @return multitype:number string
 	 */
  	public function del_rule($id)
   	{
   		$result = array('error' => 0, 'content' => '删除成功');
   		if($id)
   		{
   			$sql = "SELECT type FROM ".$this->table('rule')." WHERE id=".$id;
   			$type = $this->getOne($sql);
   			$sql = "DELETE FROM ".$this->table('rule')." WHERE  id='".$id."'"; 
   			$res = $this->query($sql);
   	   		if($type == 1)
   	   		{
   				$sql = "DELETE FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
   				$res2 = $this->query($sql);
   				if(!$res || !$res2)
   				{
   					$result['error'] = 1;
   					$result['content'] = '删除失败';
   				}
   	   		}
   	   		else
   	   		{
   	   	   		$sql = "DELETE FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
   	   	 		$res2 = $this->query($sql);
   	   	    	if(!$res||!$res2)
   	   	  		{
   	   				$result['error'] = 1;
   	   				$result['content'] = '删除失败';
   	   	  		}
   	  		}
   		}
   		return $result;
   	}
 
  	/**
  	 * 插入微信规则
  	 * @param $info
  	 * @param $content
  	 * @return 
  	 */
    public function insert_rule($info, $content)
	{
		$result=array('error' => 0, 'content' => '', 'rule_id' => 0);
		
		$data = yf_set_insert($info);
			
		$sql="INSERT INTO ".$this->table('rule')." (".$data['key'].") VALUES(".$data['val'].")";
		$this->query($sql);
		$rid = $this->insert_id();
		if(!$rid)
		{
			$result['error'] = 1;
			$result['content'] = Lang('SQL_ERROR');
		}
		else 
		{
			$result['rule_id'] = $rid;
			if($info['type'] == 1)
			{
				$sql = "INSERT INTO ".$this->table('basic_reply')." (rid,content) VALUES('$rid','$content')";
				if(!$res = $this->query($sql))
				{
					$result['error'] = 1;
					$result['content'] = Lang('SQL_ERROR');
				}
			}
			else 
			{
				if(is_array($content) && count($content) > 0)
			    {
					foreach($content as $key => $val)
					{
						$content[$key]['rid'] = $rid;
					}	

					if(!$res = $this->insert($content, 'news_reply'))
					{
					  	$result['error'] = 1;
					  	$result['content'] = Lang('SQL_ERROR');
					}
				}
			}
		}
		return $result;
	}
	
	/**
	 * 更新微信规则
	 * @param $info
	 * @param $content
	 * @param $id
	 * @return 
	 */
    public function update_rule($info, $content, $id)
	{
		$result = array('error' => 0, 'content' => '');
		if($id)
		{
			$info['modifytime'] = yf_time();
			$data = yf_set_update($info);
		
			$sql = "UPDATE ".$this->table('rule')." SET ".$data." WHERE id='".$id."'";
		    $res = $this->query($sql);
		    if(!$res)
		    {
		    	$result['error'] = 1;
		    	$result['content'] = Lang('SQL_ERROR');
		    }
		    else 
		    {
		    	if(is_array($content))
		    	{
		    	   if(is_array($content) && count($content) > 0)
		    		{
		    			
		    			foreach($content as $key => $val)
		    			{
		    				$content[$key]['rid'] = $id;
		    			}
		    			$error = 0;
		    			foreach($content as $kk => $vv)
		    			{
		    				$reply_id = "";
		    				if(!empty($vv['id']))
		    				{
		    					$reply_id = $vv['id'];
		    				}
		    				unset($vv['id']);
		    				if(!empty($reply_id))
		    				{
		    					$yf_data = yf_set_update($vv);
		    					$sql = "update ".$this->table('news_reply')." set ".$yf_data." where id=".$reply_id;
		    					if(!$res = $this->query($sql))
		    					{
		    						$error++;
		    					}
		    				}
		    				else 
		    				{
		    					$yf_data = yf_set_insert($vv);
		    					$sql = "insert into ".$this->table('news_reply')." (".$yf_data['key'].") values (".$yf_data['val'].")";
		    					if(!$res = $this->query($sql))
		    					{
		    						$error++;
		    					}
		    				}
		    			}
		    			if($error != 0)
		    			{
		    				$result['error'] = 1;
		    				$result['content'] = Lang('SQL_ERROR');
		    			}
		    		}
		    	}
		    	else 
		    	{
		    		$sql = "UPDATE ".$this->table('basic_reply')." SET content='".$content."' WHERE rid='".$id."'";
		    		$res = $this->query($sql);
		    	}
		    }
		}
		return $result;
	}
	
	/**
	 * 获取微信规则列表
	 * @param $page
	 * @return 
	 */
	public function get_rule_list($page = '', $limit = 10)
	{
		if($page)
		{
			$listPage=array('data'=>'','pageLink'=>'');
			$result=$this->show_page("SELECT id,name,type FROM ".$this->table('rule')." ORDER BY id DESC", $page, $limit);
			$listPage['pagelink']=$result['page'];
			$listPage['data']=$result['result'];
			return $listPage;
		}
		else 
		{
			$sql="SELECT name,id FROM ".$this->table('rule');
			return $this->getAll($sql);
		}	
	}
}